﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TTLManager.cs" company="Rolosoft Ltd">
//   © Rolosoft Ltd
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

#region License

// Copyright 2013 Rolosoft Ltd
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace Rsft.Net.Dns
{
    #region Usings

    using System.Collections.Generic;
    using System.Diagnostics.Contracts;

    using Rsft.Net.Dns.Interfaces;

    #endregion

    /// <summary>
    /// The TTL manager.
    /// </summary>
    /// <typeparam name="TRecord">
    /// </typeparam>
    public sealed class TTLManager<TRecord>
        where TRecord : IRecord
    {
        #region Constants

        /// <summary>
        ///     The default TTL.
        /// </summary>
        private const uint MyDefaultTTL = 86400;

        #endregion

        #region Fields

        /// <summary>
        ///     The records.
        /// </summary>
        private readonly IList<TRecord> thisRecords;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="TTLManager{TRecord}" /> class.
        /// </summary>
        /// <param name="records">The records.</param>
        public TTLManager(IEnumerable<TRecord> records)
        {
            Contract.Requires(records != null);

            this.thisRecords = new List<TRecord>();

            foreach (TRecord record in records)
            {
                this.AddItem(record);
            }
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets the default TTL.
        /// </summary>
        /// <value>The default TTL.</value>
        public uint DefaultTTL
        {
            get
            {
                return MyDefaultTTL;
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// Adds the item.
        /// </summary>
        /// <param name="record">
        /// The record.
        /// </param>
        public void AddItem(TRecord record)
        {
            this.thisRecords.Add(record);
        }

        /// <summary>
        ///     Gets the current record.
        /// </summary>
        /// <returns>
        ///     Current record.
        /// </returns>
        public TRecord GetRecord()
        {
            var thisRecord = this.thisRecords[0];

            return thisRecord == null ? default(TRecord) : thisRecord;
        }

        /// <summary>
        ///     The get TTL.
        /// </summary>
        /// <returns>
        ///     The calculated TTL of the record.
        /// </returns>
        public uint GetTTL()
        {
            var rtn = this.DefaultTTL;

            foreach (TRecord record in this.thisRecords)
            {
                // just take the 1st one
                rtn = record.ResourceRecord.TimeToLive;
                break;
            }

            return rtn;
        }

        #endregion
    }
}